﻿using lBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApplication191.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly lUserBLL db;
        private readonly IWebHostEnvironment web;
        private readonly ILogger<UserController> logger;

        public UserController(lUserBLL db , IWebHostEnvironment Web , ILogger<UserController> logger)
        {
            this.db = db;
            web = Web;
            this.logger = logger;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userinfor"></param>
        /// <returns>返回受影响函数</returns>
        [HttpPost("AddAsync")]
        public Task<int> AddAsync(Userinfor userinfor)
        {
            try
            {
                logger.LogInformation("食谱管理添加成功");
                return db.AddAsync(userinfor);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响函数</returns>
        [HttpDelete("DeleteAsync")]
        public Task<int> DeleteAsync(int id)
        {
            try
            {
                return db.DeleteAsync(id);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 显示+查询
        /// </summary>
        /// <param name="name">模糊查询查询</param>
        /// <param name="xia">下拉框查询</param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("GetAllAsync")]
        public IQueryable GetAllAsync(string? name, int? xia)
        {
            try
            {
                return db.GetAllAsync(name, xia);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userinfor"></param>
        /// <returns>返回受影响函数</returns>
        [HttpPut("UpdateAsnc")]
        public Task<int> UpdateAsnc(Userinfor userinfor)
        {
            try
            {
                return db.UpdateAsnc(userinfor);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns>返回受影响函数</returns>
        [HttpPost("PostImg")]
        public dynamic PostImg(IFormFile file)
        {
            try
            {
                string x = DateTime.Now.ToString("yyyymmdd");
                string path = web.WebRootPath + @"\Img\" + x + file.FileName;
                using (FileStream fd = System.IO.File.Create(path))
                {
                    file.CopyTo(fd);
                    fd.Close();
                }
                return "/Img/" + x + file.FileName;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 显示+查询+分页
        /// </summary>
        /// <param name="names">模糊查询</param>
        /// <param name="pageindex">总页数</param>
        /// <param name="pagesize">总条数</param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("GetAllAsyncList")]
        public dynamic GetAllAsyncList(string? names, int pageindex, int pagesize)
        {
            try
            {
                return db.GetAllAsyncList(names, pageindex, pagesize);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("LjDele")]
        public Task<int> LjDele(int id)
        {
            try
            {
                return db.LjDele(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("Plzt")]
        public Task<int> Plzt(string idstring)
        {
            try
            {
                return db.Plzt(idstring);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="shanginfor"></param>
        /// <returns>返回受影响函数</returns>
        [HttpPost("AddAsyncss")]
        public Task<int> AddAsyncss(Shanginfor shanginfor)
        {
            try
            {
                return db.AddAsyncss(shanginfor);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
